<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*,javax.sql.*,javax.naming.*"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <title>My JSP 'index.jsp' starting page</title>
    </head>

    <body>
            <%
                Context initCtx = null;
                final String JNDINAME = "java:comp/env/jdbc/MySQL";//java:comp/env/是固定的,jdbc/MySQL则是我们配置的JNDI名称
                Connection conn = null;
                try {
                    initCtx = new InitialContext();
                    DataSource ds = (DataSource) initCtx.lookup(JNDINAME);
                    conn = ds.getConnection();
                    out.println("数据连接为:" + conn);
                } catch (NamingException e) {
                    e.printStackTrace();
                } catch (SQLException e) {
                    e.printStackTrace();
                } finally {
                    if (conn != null) {
                        try {
                            conn.close();
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    }
                }
            %>
        <br>
    </body>
</html>